import 'package:flutter_screenutil/screen_util.dart';
import 'package:get/get.dart';
import 'package:lishopflu/base/view_import.dart';
import 'package:lishopflu/global/my_theme_data.dart';

import 'base_controller.dart';
import 'base_import.dart';
import 'controller_import.dart';

abstract class BasePage<T extends GetxController> extends GetView<T> {

  BuildContext _loadingContext;
  String _appTitle = 'Title';
  List<Widget> _actions;
  Color bgColor = Color(0xFFF6F6F6);
  ScreenUtil screenUtil;
  double statusBarHeight = 0.0;
  BuildContext mContext;

  void init() {
    ScreenUtil.init(BoxConstraints());
    screenUtil = ScreenUtil();
    statusBarHeight = screenUtil.statusBarHeight;
    onCreate(mContext);
  }

  @override
  Widget build(BuildContext context) {
    mContext = context;
    init();
    return Scaffold(
      resizeToAvoidBottomInset: false,
      appBar: appbar(),
      body: GestureDetector(
        behavior: HitTestBehavior.translucent,
        child: DefaultTextStyle.merge(
          child: pageBody(context),
          overflow: TextOverflow.ellipsis,
          style: MyThemeData.textStyle(),
        ),
        onTap: (){// 触摸收起键盘
          FocusScope.of(context).requestFocus(FocusNode());
        },
      ),
      backgroundColor: bgColor,
      floatingActionButton: floBtn(),
    );
  }

  //Container(color: Color.fromRGBO(0, 0, 0, 0),)

  //页面初始化
  void onCreate(BuildContext mContext);
  //页面布局
  Widget pageBody(BuildContext context);

  /*
    销毁页面
   */
  void finish({ dynamic data }){
    hideLoading();
    Get.back(result: data);
//    APPNavigator.pop(mContext, data: data);
  }

  //空布局
  Widget emptyView( {VoidCallback onCall}){
    onCall?.call();
    return Container(
      color: bgColor,
//      width: double.minPositive,
//      height: double.minPositive,
      padding: EdgeInsets.only(top: 150),
      child: Column(
        children: [
          Image.asset('images/about_github.png'),
          Padding(padding: EdgeInsets.only(top: 20)),
          Text('暂无数据'),
        ],
      ),
    );
  }

  /*
   *  公用的AppBar的title
   */
  void setAppTitle(String title, {List<Widget> actions}){
    _appTitle = title;
    _actions = actions;
  }

  /*
   *  公用的AppBar
   */
  Widget appbar(){
    return AppBar(
      title: Text(_appTitle, style: TextStyle(color: Colors.white, fontSize: 16),),
      centerTitle: true,
      leading: CommonWidget.leading(() {
        finish();
      }),
      actions: _actions,
    );
  }

  /*
   * 吐司
   */
  void toast(String msg){
    ToastUtil.toast(msg);
  }

  /*
   * snack
   */
  void snack(String msg, {String title}){
    Get.snackbar(StringUtil.text(title), StringUtil.text(msg));
  }

  /*
  * 悬浮按钮
   */
  Widget floBtn(){
    return null;
  }

  /*
   *  显示 公用的对话框-加载中...
   *  backPress：点击返回，true消失，false不消失
   *  cancelOutside： 点击加载框外区域，true消失， false不消失
   */
  void showLoading({String str, bool backPress, bool cancelOutside}){
    showDialog<Null>(
        context: mContext,
        barrierDismissible: cancelOutside==null?true:cancelOutside,
        builder: (BuildContext context) {
          _loadingContext = context;
          return WillPopScope(
            child: LoadingDialog( //调用对话框
              text: StringUtil.isEmpty(str)?'正在获取数据...':str,
            ),
            onWillPop: () async {
              return Future.value(backPress==null?true:backPress);
            },
          );
        }
    ).then((value) => {//对话框关闭后
        _loadingContext = null,
    });
  }

  /*
   *  关闭 公用的对话框-加载中...
   */
  void hideLoading(){
    if(_loadingContext != null) {//防止多次关闭
        Navigator.pop(_loadingContext);
    }
  }
}
